package com.zhsj.OkeHomeConsumption.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;


import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelpay.PayResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.zhsj.OkeHomeConsumption.ui.activity.PayResultActivity;


public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "微信支付";
    private IWXAPI api;
    private String tickid = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, "wx50199addaec9e004");
        api.handleIntent(getIntent(), this);
//        mSharePreferanceUtils = new SharedPreferanceUtils(this);
//        tickid = mSharePreferanceUtils.getTickid_str();
        //注册订阅者
//        EventBus.getDefault().register(this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }


    @Override
    public void onResp(BaseResp resp) {
        Log.e(TAG, "onPayFinish, errCode = " + resp.errCode);

        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            PayResp presp = (PayResp) resp;
            String payType = ((PayResp) resp).extData;
            if (resp.errCode == 0) {
                Intent intent = new Intent(this,PayResultActivity.class);
                startActivity(intent);
                finish();
            } else {
//                switch (payType) {
//                    case "Pay": //正常预约支付小定
//                        finish();
//
//                        break;
//                }
                Toast.makeText(this, "交易失败", Toast.LENGTH_LONG).show();
                finish();
            }
        }
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}